3 // AIUtilities.framework
5 // Created by Evan Schoenberg on 2/15/05.
6 // Copyright 2005 The Adium Team. All rights reserved.
9 #import "AIArrayAdditions.h"
11 @implementation NSArray (AIArrayAdditions)
13 - (BOOL)containsObjectIdenticalTo:(id)obj
15 return ([self indexOfObjectIdenticalTo:obj] != NSNotFound);
18 // Returns an array from the owners bundle with the specified name
19 + (NSArray *)arrayNamed:(NSString *)name forClass:(Class)inClass
21 NSBundle *ownerBundle;
25 ownerBundle = [NSBundle bundleForClass:inClass];
28 arrayPath = [ownerBundle pathForResource:name ofType:@"plist"];
30 return [[[NSArray alloc] initWithContentsOfFile:arrayPath] autorelease];
33 - (NSComparisonResult)compare:(NSArray *)other
35 NSComparisonResult result = NSOrderedSame;
37 NSEnumerator *selfEnum = [self objectEnumerator], *otherEnum = [other objectEnumerator];
38 id selfObj, otherObj = nil;
39 while ((result == NSOrderedSame) && (selfObj = [selfEnum nextObject]) && (otherObj = [otherEnum nextObject])) {
40 result = [selfObj compare:otherObj];
43 if (result == NSOrderedSame) {
44 if (selfObj && !otherObj) {
45 result = NSOrderedDescending;
46 } else if(otherObj && !selfObj) {
47 result = NSOrderedAscending;
54 - (BOOL)validateAsPropertyList
57 NSEnumerator *enumerator = [self objectEnumerator];
60 while ((value = [enumerator nextObject])) {
61 Class valueClass = [value class];
62 if (![value isKindOfClass:[NSString class]] &&
63 ![value isKindOfClass:[NSData class]] &&
64 ![value isKindOfClass:[NSNumber class]] &&
65 ![value isKindOfClass:[NSArray class]] &&
66 ![value isKindOfClass:[NSDictionary class]] &&
67 ![value isKindOfClass:[NSDate class]]) {
68 NSLog(@"** Array failed validation: %@: Value %@ is a %@ but must be a string, data, number, array, dictionary, or date",
69 self, value, NSStringFromClass(valueClass));
73 if ([value isKindOfClass:[NSArray class]] ||[value isKindOfClass:[NSDictionary class]]) {
74 BOOL successOfValue = [value validateAsPropertyList];
75 if (validated) validated = successOfValue;
84 @implementation NSMutableArray (ESArrayAdditions)
86 - (void)addObjectsFromArrayIgnoringDuplicates:(NSArray *)inArray
88 NSEnumerator *enumerator = [inArray objectEnumerator];
91 while ((object = [enumerator nextObject])) {
92 if (![self containsObject:object]) [self addObject:object];
96 - (void)moveObject:(id)object toIndex:(unsigned)newIndex
98 unsigned currentIndex = [self indexOfObject:object];
99 NSAssert3(currentIndex != NSNotFound, @"%@ %p does not contain object %p", NSStringFromClass([self class]), self, object);
101 //if we're already there, do no work
102 if (currentIndex == newIndex) return;
104 //Account for shifting
105 if (currentIndex < newIndex) newIndex--;
107 //Move via a remove and add :(
109 [self removeObject:object];
110 [self insertObject:object atIndex:newIndex];
114 //just a better name for an existing NSMutableArray method.
115 //this makes it uniform in style with -[NSMutableDictionary setObject:forKey:].
116 - (void)setObject:(id)object atIndex:(unsigned)index
118 [self replaceObjectAtIndex:index withObject:object];